package com.yaorange.web;

import com.github.pagehelper.PageInfo;
import com.yaorange.entity.Customer;
import com.yaorange.entity.Emp;
import com.yaorange.service.CustomerService;
import com.yaorange.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/business/invalid")
@CrossOrigin
public class BusinessInvalidConcroller {


    @Autowired
    private CustomerService customerService;
    @Autowired
    private EmpService empService;

    //获取需要进行分配的客户
    @GetMapping("/page/{pageNum}/{pageSize}/{account}")
    public PageInfo<Customer> page(@PathVariable Integer pageNum,
                                   @PathVariable Integer pageSize, @PathVariable String account){
        //通过账户获取员工
        Emp emp = empService.getEmpByUserName(account);
        System.out.println(emp.getDeptId());
        if(emp == null){
            return null;
        }
        System.out.println(emp.getDeptId());
        return customerService.getPageByDeptId(pageNum,pageSize,emp.getDeptId());
    }



    //获取需要进行审核的客户
    @GetMapping("/Invalidpage/{pageNum}/{pageSize}/{account}")
    public PageInfo<Customer> Invalidpage(@PathVariable Integer pageNum,
                                          @PathVariable Integer pageSize, @PathVariable String account){
        //通过账户获取员工
        Emp emp = empService.getEmpByUserName(account);
        if(emp == null){
            return null;
        }
        //System.out.println(emp.getDeptId());
        return customerService.getInvalidPageByDeptId(pageNum,pageSize,emp.getDeptId());
    }


    //获取所有员工
    @GetMapping("/emp/{account}")
    public List<Emp> getSourceList(@PathVariable String account){
        Emp emp = empService.getEmpByUserName(account);
        List<Emp> empList = empService.getEmpListByDeptId(emp.getDeptId());
        return empList;
    }


    @GetMapping("/{id}")
    public Customer customer(@PathVariable Integer id){
        return customerService.getCustomerById(id);
    }


    //设置客户为无效状态
    @PutMapping("/InvalidCustomer")
    public Map<String,Object> InvalidCustomer(@RequestBody Customer customer){
        Map<String, Object> map = new HashMap<>();
        //设置新订单客户更新时的默认字段值
        //设置客户状态为无效
        customer.setState(10);
        int rows = customerService.updateBySelective(customer);
        if(rows > 0){
            map.put("status",200);
            map.put("msg","设置无效成功");
        }else{
            map.put("msg","设置无效失败");
        }
        return map;
    }


    //分配客户给指定员工
    @PutMapping("/alloction/{empId}")
    public Map<String,Object> alloction(@RequestBody Customer customer, @PathVariable Integer empId){
        Map<String, Object> map = new HashMap<>();
        //设置客户的归属员工
        customer.setState(2);
        customer.setEmpId(empId);
        int rows = customerService.updateBySelective(customer);
        if(rows > 0){
            map.put("status",200);
            map.put("msg","分配成功");
        }else{
            map.put("msg","分配失败");
        }
        return map;
    }


    //将客户设为删除状态
    @PutMapping("/deleteCustomer")
    public Map<String,Object> deleteCustomer(@RequestBody Customer customer){
        Map<String, Object> map = new HashMap<>();
        //设置新订单客户更新时的默认字段值
        //设置客户状态为删除
        customer.setState(11);
        int rows = customerService.updateBySelective(customer);
        if(rows > 0){
            map.put("status",200);
            map.put("msg","客户删除成功");
        }else{
            map.put("msg","客户删除失败");
        }
        return map;
    }


    //驳回客户
    @PutMapping("/reject")
    public Map<String,Object> reject(@RequestBody Customer customer){
        Map<String, Object> map = new HashMap<>();
     //驳回后客户状态修改
        customer.setState(3);
        int rows = customerService.updateBySelective(customer);
        if(rows > 0){
            map.put("status",200);
            map.put("msg","成功驳回");
        }else{
            map.put("msg","驳回失败");
        }
        return map;
    }




}
